package com.spring.security.config.security;

import com.spring.security.project.po.UserPO;
import com.spring.security.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
 * @Author wsh
 * @Date 2020/7/16 13:39
 * @Desc 自定义用户校验
 */
@Component(value = "userDetailService")
public class CustomUserDetailService implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException {
        if (StringUtils.isEmpty(name)){
            throw new UsernameNotFoundException("用户名不能为空！");
        }
        UserPO userInfo = userService.getUserInfo(name);
        if (ObjectUtils.isEmpty(userInfo)){
            throw new UsernameNotFoundException("用户不存在！");
        }
        return userInfo;
    }
}
